from typing import Dict, List, Optional
import logging
from .base import BaseDataFeed
from .mock_feed import MockDataFeed
from common.data_models import DataType

class DataFeedManager:
    """数据源管理器"""
    
    def __init__(self):
        self.feeds: Dict[str, BaseDataFeed] = {}
        self.logger = logging.getLogger("data_feed.manager")
        
    def create_feed(self, feed_type: str, config: Dict) -> str:
        """创建数据源"""
        feed_id = f"{feed_type}_{len(self.feeds) + 1}"
        
        if feed_type == "mock":
            feed = MockDataFeed(config)
        else:
            raise ValueError(f"不支持的数据源类型: {feed_type}")
            
        self.feeds[feed_id] = feed
        self.logger.info(f"创建数据源: {feed_id}")
        return feed_id
        
    def connect(self, feed_id: str) -> bool:
        """连接数据源"""
        if feed_id not in self.feeds:
            self.logger.error(f"数据源不存在: {feed_id}")
            return False
            
        feed = self.feeds[feed_id]
        success = feed.connect()
        
        if success:
            self.logger.info(f"数据源连接成功: {feed_id}")
        else:
            self.logger.error(f"数据源连接失败: {feed_id}")
            
        return success
        
    def subscribe(self, feed_id: str, symbols: List[str], data_type: DataType = DataType.TICK):
        """订阅数据"""
        if feed_id not in self.feeds:
            raise ValueError(f"数据源不存在: {feed_id}")
            
        feed = self.feeds[feed_id]
        feed.subscribe(symbols, data_type)
        
    def get_health_status(self) -> Dict:
        """获取所有数据源健康状态"""
        status = {}
        for feed_id, feed in self.feeds.items():
            status[feed_id] = feed.health_check()
        return status